import {
  SysDictValueAdd,
  SysDictValueData,
  SysDictValueDetail,
  SysDictValueParams
} from "@/pages/system/dict/value/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";


export const sysDictValuePageList = async (sysDictValueParams: SysDictValueParams) => {
  return request<{
    data: {
      records: SysDictValueData[],
      total: number,
    }
    success: boolean
  }>('/api/sys/dict/value/list', {
    method: 'POST',
    data: sysDictValueParams
  });
}


export const sysDictValueUpdate = async (sysDictValueAdd: SysDictValueAdd) => {
  return request<{
    data: string;
  }>('/api/sys/dict/value/update', {
    method: 'POST',
    data: sysDictValueAdd
  });
}

export const sysDictValueAdd = async (sysDictValueAdd: SysDictValueAdd) => {
  return request<{
    data: string;
  }>('/api/sys/dict/value/add', {
    method: 'POST',
    data: sysDictValueAdd
  });
}

export const sysDictValueRemove = async (ids: (number | string)[]) => {
  return request<{
    data: string;
  }>('/api/sys/dict/value/remove', {
    method: 'POST',
    data: {
      ids
    }
  });
}

export const sysDictValueDetail = async (id: number) => {
  return request<{
    data: SysDictValueDetail;
  }>('/api/sys/dict/value/detail', {
    method: 'POST',
    data: {
      id
    }
  });
}

export const sysDictValueImport = async (formData: FormData) => {
  return request<{
    data: string;
  }>('/api/sys/dict/value/import', {
    method: 'POST',
    data: formData,
    requestType: "form"
  });
}


const sysDictValueExport = async (data: { ids: (number | string)[], id: number }) => {
  return request<{
    data: string;
  }>('/api/sys/dict/value/export', {
    method: 'POST',
    data: data,
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysDictValueExport = async (data: { ids: (number | string)[], id: number }) => {
  await sysDictValueExport(data).then(({data, response}) => {
    download(data, response)
  });
}


const sysDictValueExportTemplate = async () => {
  return request<{
    data: string;
  }>('/api/sys/dict/value/export/template', {
    method: 'POST',
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysDictValueExportTemplate = async () => {
  await sysDictValueExportTemplate().then(({data, response}) => {
    download(data, response)
  });
}
